//
//  UIViewController+Extension.swift
//  EasyDana
//
//  Created by mac on 2024/10/9.
//

import UIKit
import SVProgressHUD
extension UIViewController {
    @MainActor
    func alert(viewController: UIViewController) {
        self.definesPresentationContext = true
        viewController.view.backgroundColor = UIColor.black.withAlphaComponent(0.3)
        viewController.modalPresentationStyle = .overFullScreen
        self.present(viewController, animated: false)
    }
    
    @MainActor
    func showLoading(text: String?) {
        SVProgressHUD.show(withStatus: text)
    }
    
    @MainActor
    func hideLoading() {
        SVProgressHUD.dismiss()
    }
    
    @MainActor
    func show(toast: String?) {
        SVProgressHUD.showInfo(withStatus: toast)
        SVProgressHUD.dismiss(withDelay: 2.0)
    }
}

extension UIViewController {
    
    @discardableResult
    func jump(url: URL, notJump: Bool = false) -> UIViewController {
        if url.absoluteString.hasPrefix("http") {
            let webVC = WebViewController()
            webVC.url = url.absoluteString
            webVC.hidesBottomBarWhenPushed = true
            if notJump == false {
                self.navigationController?.pushViewController(webVC, animated: true)
            }
            return webVC
        } else if (url.absoluteString.hasPrefix("ea://sy.dan.a/")) {
            var queries = ""
            var path = ""
            if #available(iOS 16.0, *) {
                queries = url.query(percentEncoded: true) ?? ""
                path = url.path(percentEncoded: true)
            } else {
                queries = url.query ?? ""
                path = url.path
            }
            let params = queries.components(separatedBy: "&")
            switch path {
            case "/interest":
                let settingVC = SettingViewController()
                if notJump == false {
                    settingVC.hidesBottomBarWhenPushed = true
                    self.navigationController?.pushViewController(settingVC, animated: true)
                }
                return settingVC
            case "/code":
                self.navigationController?.popToRootViewController(animated: false)
                self.tabBarController?.selectedIndex = 0
                break
            case "/stopped":
                let loginVC = LoginViewController()
                if notJump == false {
                    UIViewController.top?.alert(viewController: loginVC)
                }
                return loginVC
            case "/preach":
                if let first = params.first {
                    let kv = first.components(separatedBy: "=")
                    let value = Int(kv.last ?? "") ?? 0
                    let statuses = [0: 4, 1: 7, 2: 6, 3: 5]
                    let status = statuses[value] ?? 4
                    if let secondNavi = self.tabBarController?.viewControllers?[1] as? UINavigationController, let ordersVC = secondNavi.viewControllers.first as? OrdersViewController {
                        ordersVC.status = status
                        self.tabBarController?.tabBar.isHidden = false
                        self.tabBarController?.selectedIndex = 1
                        self.navigationController?.popToRootViewController(animated: false)
                    }
                    return UIViewController()
                }
                break
            case "/useful":
                if let first = params.first {
                    let kv = first.components(separatedBy: "=")
                    let value = kv.last ?? ""
                    let processVC = LoanProcessViewController()
                    processVC.id = value
                    processVC.hidesBottomBarWhenPushed = true
                    if notJump == false {
                        self.navigationController?.pushViewController(processVC, animated: true)
                    }
                    return processVC
                }
            default:
                return UIViewController()
            }
        }
        return UIViewController()
    }
}

extension UIViewController {
    static var top: UIViewController? {
        guard let keyWindow = UIApplication.shared.connectedScenes
                .filter({ $0.activationState == .foregroundActive })
                .compactMap({ $0 as? UIWindowScene })
                .first?.windows
                .filter({ $0.isKeyWindow }).first else {
                return nil
            }
            
        var topController = keyWindow.rootViewController as! UITabBarController
        let selectedNavi = topController.selectedViewController as? UINavigationController
        let last = selectedNavi?.viewControllers.last
        if ((last?.presentedViewController) != nil) {
            return last!.presentedViewController
        } else {
            return last
        }
    }
}
